package com.yckj.school.teacherClient.ui.Bside.home.shortVideo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.model.VideoOptionModel;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.yckj.school.teacherClient.R;
import com.yckj.school.teacherClient.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * 播放rtsp视频Activity
 */
public class RtspVideoActivity extends AppCompatActivity {

    StandardGSYVideoPlayer videoPlayer;

    OrientationUtils orientationUtils;

    String url = "";
    String title = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtmp);
        initView();
        initData();
        initListener();
    }



    private void initData() {
        PlayerFactory.setPlayManager(IjkPlayerManager.class);

        VideoOptionModel videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "rtsp_transport", "tcp");
        List<VideoOptionModel> list = new ArrayList<>();
        list.add(videoOptionModel);
        GSYVideoManager.instance().setOptionModelList(list);

        //增加封面
//        ImageView imageView = new ImageView(this);
//        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        imageView.setImageResource(R.mipmap.icon_new);
//        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);




        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");

        if (url == null || url.equals("")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("播放提示");
            builder.setMessage("播放链接异常，请您退出重新尝试");
            builder.setNegativeButton("确认", (dialog, which) -> {
                finish();
            });
        }

        //-1未联网  1wifi 2手机网
        if (isWifi(this) == 2) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("温馨提示");
            dialog.setCancelable(false);
            dialog.setMessage("您处于手机流量模式下，是否要继续观看?");
            dialog.setPositiveButton("继续观看", (dialog1, which) -> {
                        videoPlayer.setUp(url, true, title);
                        videoPlayer.startPlayLogic();
                    }
            );
            dialog.setNegativeButton("再考虑下", (dialog12, which) -> {
                dialog12.dismiss();
                finish();
            });
            dialog.show();
        } else if (isWifi(this) == 1) {
            videoPlayer.setUp(url, true, title);
            videoPlayer.startPlayLogic();
        } else {
            Toast.makeText(this, "请您检查手机网络是否正常", Toast.LENGTH_SHORT).show();
        }
    }

    private void initListener() {
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orientationUtils.resolveByClick();
            }
        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(false);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    /**
     * 跳转进入
     *
     * @param activity
     * @param url
     * @param title
     */
    public static void startActivity(Activity activity, String url, String title) {
        Intent intent = new Intent(activity, RtspVideoActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        activity.startActivity(intent);
    }


    private void initView() {
        videoPlayer = findViewById(R.id.video_player);
    }


    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //先返回正常状态
        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.getFullscreenButton().performClick();
            return;
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }


    /**
     * 判断wifi
     * -1未联网 1wifi 2手机网
     *
     * @param activity
     */
    public static int isWifi(Activity activity) {
        ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo == null) {
            return -1;
        }
        if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return 1;
        } else {
            return 2;
        }
    }
}